/**
 * SessionStorage.java
 * @author: liuex
 * 2008-1-7 ����07:39:02
 *
 * note: 
 */
package org.lex.app;

import java.awt.Component;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

import org.lex.app.prefs.FileBasedResourceMap;
import org.lex.app.prefs.PropertyFileResourceMap;
import org.lex.app.sessions.Session;
import org.lex.app.sessions.SessionStorageHelper;


public class SessionStorage {
	private static final String SESSION_STORAGE_FILE = "sessions.properties";
	// ======================================================================
	private FileBasedResourceMap sessions = null;

	public static <T extends Session> void registerSession(Class<? extends Component> clz, T session) {
		SessionStorageHelper.registerSession(clz, session);
	}

	public static Set<Map.Entry<Class<?>, Session>> availableSessions() {
		return SessionStorageHelper.availableSessions();
	}

	public SessionStorage(File apphome, String appid) {
		File file = new File(apphome, SESSION_STORAGE_FILE);
		sessions = new PropertyFileResourceMap(file);
		sessions.setComment(appid + " session storage file");
		try {
			sessions.load();
		} catch (IOException e) {
			System.err.println("load session error: " + e.getMessage());
		}
	}

	public void save(Component root) {
		SessionStorageHelper.save(root, sessions);
		try {
			this.sessions.store();
		} catch (IOException e) {
			System.err.println("store sessions error: " + e.getMessage());
		}
	}

	public void restore(Component root) {
		SessionStorageHelper.restore(root, sessions);
	}
}
